﻿using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using Postal;
using Hangfire;
using System;

namespace SAIM.Models
{
    public class NewCommentEmail : Email
    {
        public string To { get; set; }
        public string UserName { get; set; }
        public string Message { get; set; }
        public string TicketId { get; set; }
        public string CommentDate { get; set; }
        public string urlTicket { get; set; }

        [AutomaticRetry(Attempts = 3)]
        public static void NotifyNewComment(TicketComment model, string url, List<string> usersEmails)
        {
            string emails = string.Empty;
 
            foreach(string userEmail in usersEmails)
            {
                if((userEmail != string.Empty) && (userEmail != null))
                {
                    emails += userEmail + ",";
                }
            }
            emails = emails.Remove(emails.Length - 1);
            var email = new NewCommentEmail
            {
                To = emails,
                UserName = model.UserName,
                Message = model.Comment,
                TicketId = model.IdTicket.ToString(),
                CommentDate = model.CommentDate.ToString(),
                urlTicket = url
            };
            email.Send();
        }
    }

    public class CommentEditionEmail : Email
    {
        public string To { get; set; }
        public string UserName { get; set; }
        public string Message { get; set; }
        public string TicketId { get; set; }
        public string CommentDate { get; set; }
        public string urlTicket { get; set; }

        [AutomaticRetry(Attempts = 3)]
        public static void NotifyCommentEdition(TicketComment model, string url, List<string> usersEmails)
        {
            string emails = string.Empty;

            foreach (string userEmail in usersEmails)
            {
                if((userEmail != string.Empty) && (userEmail != null))
                {
                    emails += userEmail + ",";
                }
            }
            emails = emails.Remove(emails.Length - 1);
            var email = new CommentEditionEmail
            {
                To = emails,
                UserName = model.UserName,
                Message = model.Comment,
                TicketId = model.IdTicket.ToString(),
                CommentDate = model.CommentDate.ToString(),
                urlTicket = url
            };
            email.Send();
        }
    }

    public class FileUploadedEmail : Email
    {
        public string To { get; set; }
        public string UserName { get; set; }
        public string TicketId { get; set; }
        public string CommentDate { get; set; }
        public string urlTicket { get; set; }

        [AutomaticRetry(Attempts = 3)]
        public static void NotifyFileUploaded(TicketComment model, string url, List<string> usersEmails)
        {
            string emails = string.Empty;

            foreach (string userEmail in usersEmails)
            {
                if ((userEmail != string.Empty) && (userEmail != null))
                {
                    emails += userEmail + ",";
                }
            }
            emails = emails.Remove(emails.Length - 1);
            var email = new FileUploadedEmail
            {
                To = emails,
                UserName = model.UserName,
                TicketId = model.IdTicket.ToString(),
                CommentDate = model.CommentDate.ToString(),
                urlTicket = url 
            };
            email.Send();
        }
    }

    public class NewTicketEmail : Email
    {
        public string To { get; set; }
        public string UserName { get; set; }
        public string TicketId { get; set; }
        public string TicketUrl { get; set; }
        public string Subject { get; set; }
        public string details { get; set; }

        [AutomaticRetry(Attempts = 3)]
        public static void NotifyNewTicket(NewTicketsViewModel model, long ticketId, string url, List<string> usersEmails)
        {
            if (usersEmails.Count > 0)
            {
                string emails = string.Empty;
                string ticketSubject = string.Empty;

                foreach (System.Web.Mvc.SelectListItem item in model.Issues)
                {
                    if (item.Value == model.Issue.ToString())
                        ticketSubject = item.Text;
                }
                foreach (string userEmail in usersEmails)
                {
                    if((userEmail != string.Empty) && (userEmail != null))
                    {
                        emails += userEmail + ",";
                    }
                }
                emails = emails.Remove(emails.Length - 1);
                var email = new NewTicketEmail
                {
                    To = emails,
                    TicketId = ticketId.ToString(),
                    TicketUrl = url,
                    Subject = ticketSubject,
                    UserName = model.User,
                    details = model.Details
                };
                email.Send();
            }
        }
    }

    public class UserLoginEmail : Email
    {
        public string To { get; set; }
        public string UserName { get; set; }
        public string UserId { get;set; }
        public string UserUrl { get; set; }

        [AutomaticRetry(Attempts = 3)]
        public static void NotifyUserLogin(string userName, string userId, string url, List<string> usersEmails)
        {
            if (usersEmails.Count > 0)
            {
                string emails = string.Empty;

                foreach (string userEmail in usersEmails)
                {
                    if ((userEmail != string.Empty) && (userEmail != null))
                    {
                        emails += userEmail + ",";
                    }
                }
                emails = emails.Remove(emails.Length - 1);
                var email = new UserLoginEmail
                {
                    To = emails,
                    UserName = userName,
                    UserId = userId,
                    UserUrl = url + "?id=" + userId
                };
                email.Send();
            }
        }
    }

    public class TicketUserAddedEmail : Email
    {
        public string To { get; set; }
        public string UserName { get; set; }
        public string Url { get; set; }
        public string TicketId { get; set; }

        [AutomaticRetry(Attempts = 3)]
        public static void NotifyUserAdded(string userName, string ticketId, string url, List<string> usersEmails)
        {
            if (usersEmails.Count > 0)
            {
                string emails = string.Empty;

                foreach (string userEmail in usersEmails)
                {
                    if ((userEmail != string.Empty) && (userEmail != null))
                    {
                        emails += userEmail + ",";
                    }
                }
                emails = emails.Remove(emails.Length - 1);
                var email = new TicketUserAddedEmail
                {
                    To = emails,
                    UserName = userName,
                    Url = url,
                    TicketId = ticketId
                };
                email.Send();
            }
        }
    }

    public class TicketUserRemovedEmail : Email
    {
        public string To { get; set; }
        public string UserName { get; set; }
        public string Url { get; set; }
        public string TicketId { get; set; }

        [AutomaticRetry(Attempts = 3)]
        public static void NotifyUserRemoved(string userName, string ticketId, string url, List<string> usersEmails)
        {
            if (usersEmails.Count > 0)
            {
                string emails = string.Empty;

                foreach (string userEmail in usersEmails)
                {
                    if ((userEmail != string.Empty) && (userEmail != null))
                    {
                        emails += userEmail + ",";
                    }
                }
                emails = emails.Remove(emails.Length - 1);
                var email = new TicketUserRemovedEmail
                {
                    To = emails,
                    UserName = userName,
                    Url = url,
                    TicketId = ticketId
                };
                email.Send();
            }
        }
    }
}